package com.jinbo.footmessage.lookServers;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;


import com.jinbo.footmessage.BaseActivity;
import com.jinbo.footmessage.R;
import com.jinbo.footmessage.httpbean.WorkerInformationBean;
import com.jinbo.footmessage.utils.CharacterParser;
import com.jinbo.footmessage.utils.PinyinComparator;
import com.jinbo.footmessage.view.SideBar;
import com.jinbo.footmessage.view.TitleBarView;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Created by Administrator on 2015/3/10.
 */
public class LookServersActivity extends BaseActivity implements View.OnClickListener {
    private TitleBarView tbv_titleBar;
    private ListView mListView;
    private View mHeader,mFooter;
    private TextView mCountTv,mDialog;
    private SideBar mSb;
    private ServerAdapter mAdapter;
    private ArrayList<WorkerInformationBean> mServersList;
   // private ArrayList<ServersInfosBean> mStarList;
   // private LocalPreferencesHelper localPreferencesHelper ;
    /**
     * 汉字转换成拼音的类
     */
    private CharacterParser characterParser;

    /**
     * 根据拼音来排列ListView里面的数据类
     */
    private PinyinComparator pinyinComparator;
    private static String SERVERS_INTENT="servers";
    public static void launch(Activity activity,List<WorkerInformationBean> list) {
        Intent intent = new Intent(activity,LookServersActivity.class);
        Bundle bundle = new Bundle();
        bundle.putSerializable(SERVERS_INTENT, (Serializable)list);
        intent.putExtras(bundle);
        activity.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_servers);
        initView();
       // EventBusUtil.getInstance().getCommonEventBus().register(LookServersActivity.this);
        initData();
        initTitle();
        initListener();
    }

    private void initListener() {
   /*     mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                //这里要利用adapter.getItem(position)来获取当前position所对应的对象
           *//*     ServersInfosBean bean = mAdapter.getItem(position);
                if (bean.uid == mAdapter.getNewFrienduid()) {
                    mAdapter.setNewFrienduid(0);
                    mAdapter.notifyDataSetChanged();
                }
                GoodFriendInfoDetailActivity.launch(LookServersActivity.this, bean.uid);*//*
            }
        });*/
      /*  mListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                final int select = position;
                new DialogUtils(LookServersActivity.this, "center", true).setItems(new String[]{"删除好友"}, new DialogUtils.OnDialogItemClickListener() {
                    @Override
                    public void OnDialogItemClick(Context context, DialogUtils builder, Dialog dialog, int id) {
                        dialog.dismiss();
                        new DialogUtils(LookServersActivity.this, DialogUtils.LAYOUT_CENTER, true).setTitle("提示").setMessage("确定删除该好友吗？").setButtons("取消", "确定", new DialogUtils.OnDialogButtonClickListener() {
                            @Override
                            public void onDialogButtonClick(Context context, DialogUtils builder, Dialog dialog, int dialogId, int which) {
                                if (which == DialogUtils.OnDialogButtonClickListener.BUTTON_RIGHT) {
                                    final ServersInfosBean bean = mAdapter.getItem(select);
                                    FriendsBusinessController.getInstance().deleteFriend(localPreferencesHelper.getString(LocalPreferencesHelper.ACCESS_TOKEN), bean.uid, new Listener<SuccessNoReturnBean>() {
                                        @Override
                                        public void onStart(Object... params) {
                                            showProgressDialog("请稍后...");
                                        }

                                        @Override
                                        public void onComplete(SuccessNoReturnBean result, Object... params) {
                                            dismissProgressDialog();
                                            mServersList.remove(bean);
                                            mAdapter.updateListView(mServersList);
                                        }

                                        @Override
                                        public void onFail(String msg, Object... params) {
                                            dismissProgressDialog();
                                            ToastUtils.show(LookServersActivity.this, msg);
                                        }
                                    });
                                }
                            }
                        }).create().show();

                    }
                }).create().show();

                return true;
            }
        });*/
    }

    private void initTitle() {
        tbv_titleBar = (TitleBarView) findViewById(R.id.titleBar);
        tbv_titleBar.setCenterTitle("技师");
        tbv_titleBar.setLeftBackButtonIsVisibler(true);
        tbv_titleBar.setListener(TitleBarView.TitleBarEnum.ll_leftBackImageLayout, this);
    }

    private void initData() {
       // List<WorkerInformationBean> workers
        mServersList=(ArrayList<WorkerInformationBean>)getIntent().getSerializableExtra(SERVERS_INTENT);
        //String [] strs={"房锦波","去房","额","他","会给","不错","离开","模拟","这些","成本","删除","规划","会给","科技","地方","文档","艾丝凡","破","与人","发"};
/*        for(int i=0;i<workers.size();i++)
        {
            WorkerInformationBean bean=new WorkerInformationBean();
            bean.setId(workers.get(i).id);
            bean.setName(workers.get(i).name);
            bean.setOrder_number(workers.get(i).order_number);
            bean.setHead(workers.get(i).head);
            bean.setSpeciality(workers.get(i).speciality);
            bean.setAge(workers.get(i).age);
            bean.setSex(workers.get(i).sex);
            bean.setStar(workers.get(i).star);
            mServersList.add(bean);
        }*/

        if (mServersList.size() > 0) {
            filledData(mServersList);
            // 根据a-z进行排序源数据
            Collections.sort(mServersList, pinyinComparator);
            mCountTv.setText(mServersList.size() + "位");
            mAdapter.updateListView(mServersList);
        }

/*
        FriendsBusinessController.getInstance().getFriendList(localPreferencesHelper.getString(LocalPreferencesHelper.ACCESS_TOKEN), new Listener<FriendListItemBean>() {

            @Override
            public void onStart(Object... params) {
                showProgressDialog("请稍后...");
            }

            @Override
            public void onComplete(FriendListItemBean result, Object... params) {
                dismissProgressDialog();
                mServersList = result.infos;
                if (mServersList.size() > 0) {
                    filledData(mServersList);
                    // 根据a-z进行排序源数据
                    Collections.sort(mServersList, pinyinComparator);
                    mFriendsCountTv.setText(mServersList.size() + "位好友");
                    mAdapter.updateListView(mServersList);
                } else {
                    mFriendsCountTv.setText("暂时没有好友");
                }

            }

            @Override
            public void onFail(String msg, Object... params) {
                dismissProgressDialog();
                ToastUtils.show(LookServersActivity.this, msg);
            }

        });*/
    }

    private void initView() {
        //实例化汉字转拼音类
        characterParser = CharacterParser.getInstance();
        pinyinComparator = new PinyinComparator();
      //  localPreferencesHelper = new LocalPreferencesHelper(LookServersActivity.this, LocalPreferencesHelper.DB_NAME);

        mListView=(ListView)findViewById(R.id.lv_friends);
        mSb=(SideBar)findViewById(R.id.sidrbar);
        mDialog=(TextView)findViewById(R.id.dialog);

        mSb.setTextView(mDialog);

        //设置右侧触摸监听
        mSb.setOnTouchingLetterChangedListener(new SideBar.OnTouchingLetterChangedListener() {

            @Override
            public void onTouchingLetterChanged(String s) {
                if (s.equals("")) {
                    mListView.setSelection(0);
                    return;
                }
                //该字母首次出现的位置
                int position = mAdapter.getPositionForSection(s.charAt(0));
                if (position != -1) {
                    mListView.setSelection(position);
                }

            }
        });

        LayoutInflater inflater= LayoutInflater.from(LookServersActivity.this);
      //  mHeader=inflater.inflate(R.layout.head_my_friends,null);
        mFooter=inflater.inflate(R.layout.list_servers_foot,null);
        mCountTv=(TextView)mFooter.findViewById(R.id.tv_friends_count);
     //   mHeader.findViewById(R.id.ll_new_friend).setOnClickListener(this);
        mListView.addFooterView(mFooter);
      //  mListView.addHeaderView(mHeader);
        mServersList=new ArrayList<WorkerInformationBean>();
        mAdapter=new ServerAdapter(LookServersActivity.this,mServersList);
        mListView.setAdapter(mAdapter);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.left_back_layout:
                finish();
                break;
 /*           case R.id.ll_new_friend:
                NewFriendListActivity.launch(LookServersActivity.this);
                break;*/
        }
    }
    /**
     * 为ListView填充数据，设置model中的首字母
     * @param
     * @return
     */
    private List<WorkerInformationBean> filledData(List<WorkerInformationBean> data){
        for(int i=0; i<data.size(); i++){
            //汉字转换成拼音
            String pinyin = characterParser.getSelling(data.get(i).name);
            String sortString = pinyin.substring(0, 1).toUpperCase();
            // 正则表达式，判断首字母是否是英文字母
            if(sortString.matches("[A-Z]")){
                data.get(i).sortLetters = sortString.toUpperCase();
            }else{
                data.get(i).sortLetters = "#";
            }
        }
        return data;
    }

  /*  public void onEventMainThread(final PassFriendEvent event) {
        mAdapter.setNewFrienduid(event.uid);
        initDate();
    }*/
    @Override
    protected void onDestroy() {
        super.onDestroy();
      //  EventBusUtil.getInstance().getCommonEventBus().unregister(LookServersActivity.this);
    }
}